package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.face.FaceGetScheduling;
import com.xmy.cultivate.enums.ReachTypeEnum;
import com.xmy.cultivate.mapper.CourseMapper;
import com.xmy.cultivate.mapper.CourseSchedulingDetailMapper;
import com.xmy.cultivate.mapper.GradeMapper;
import com.xmy.cultivate.mapper.RepairCourseMapper;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class FaceStudentServiceImpl implements IFaceStudentService {


    @Autowired
    CourseSchedulingDetailMapper courseSchedulingDetailMapper;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    IStudentSignInService iStudentSignInService;

    @Autowired
    IStudentSignInTempService iStudentSignInTempService;

    @Autowired
    GradeMapper gradeMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    RepairCourseMapper repairCourseMapper;

    @Autowired
    ICourseSchedulingDetailService iCourseSchedulingDetailService;

    @Autowired
    IBrushSignSchoolService iBrushSignSchoolService;

    @Autowired
    IStaffService iStaffService;

    @Autowired //如果给redis中保存数据会使用默认的序列化机制，导致redis中保存的对象不可视
    RedisTemplate<Object, Object> redisTemplate;
    @Autowired
    private IdWorker idWorker;

    /**
     * 获取今日上课信息
     * @param schoolId
     * @param studentId
     * @return
     */
    @Override
    public FaceGetScheduling getCourseSchedulingDetail(Long schoolId, Long teacherId, Long studentId, String subjectsId, Integer lessonType, Quarter quarter) {
        String classTypeIdKey = "signInScheduling::"+studentId+""+subjectsId+""+lessonType;
        Object signInSchedulingObj = redisTemplate.opsForValue().get(classTypeIdKey);
        FaceGetScheduling faceGetScheduling = null;
        if (Objects.isNull(signInSchedulingObj)) {
            faceGetScheduling =  this.getSchedulingDetail(schoolId,teacherId, studentId,quarter,subjectsId,lessonType);
            if (!Objects.isNull(faceGetScheduling)){
                if (faceGetScheduling.getType() != null){
                    if (faceGetScheduling.getType().equals(1) || faceGetScheduling.getType().equals(3)){
                        if (faceGetScheduling.getSignInId() == null){
                            faceGetScheduling.setSignInId(idWorker.nextId());//添加id
                        }
                        redisTemplate.opsForValue().set(classTypeIdKey,faceGetScheduling,6, TimeUnit.SECONDS);
                    }
                }
            }
        }else {
            faceGetScheduling = (FaceGetScheduling) signInSchedulingObj;
            //如果不是签到重新获取
            if (faceGetScheduling.getType() != null){
                if (!faceGetScheduling.getType().equals(1) && !faceGetScheduling.getType().equals(3)){
                    faceGetScheduling =  this.getSchedulingDetail(schoolId,teacherId, studentId,quarter,subjectsId,lessonType);
                    if (!Objects.isNull(faceGetScheduling)){
                        if (faceGetScheduling.getType() != null){
                            if (faceGetScheduling.getType().equals(1)){
                                if (faceGetScheduling.getSignInId() == null){
                                    faceGetScheduling.setSignInId(idWorker.nextId());//添加id
                                }
                                redisTemplate.opsForValue().set(classTypeIdKey,faceGetScheduling,6, TimeUnit.SECONDS);
                            }
                        }
                    }
                }
            }else {
                if (faceGetScheduling.getType() != null){
                    if (faceGetScheduling.getType().equals(1)){
                        //如果是签到返回已签到状态
                        faceGetScheduling.setType(3);
                    }
                }
            }
        }

        /*if (faceGetScheduling.getIsToNext()){
            FaceGetScheduling faceGetSchedulingRepair = this.getRepairCourseDetail(schoolId,studentId,quarter);
            //存在排课信息
            if(!faceGetSchedulingRepair.getIsToNext()){
                faceGetScheduling = faceGetSchedulingRepair;
            }
        }*/

        /**
         * 没有找到课程，暂时设置为签到或者签退
         */
        /*if (faceGetScheduling.getIsToNext() && faceGetScheduling.getType() == null){

        }*/
        return faceGetScheduling;
    }

    @Override
    public List<RepairCourse> getCourseSchedulingDetailTest(Long schoolId, Long studentId) {
        LocalDate nowDate = LocalDate.now();
        QueryWrapper<RepairCourse> repairCourseQueryWrapper = new QueryWrapper<>();
        repairCourseQueryWrapper.eq("school_date",nowDate);
        repairCourseQueryWrapper.eq("school_id",schoolId);
        repairCourseQueryWrapper.eq("deleted",0);
        repairCourseQueryWrapper.and(i->i.inSql("grade_id","select grade_id from student_grade where school_id = "+schoolId+" and status = 1 and reading_status=1 and year_part = 2024 and quarter_num= 2 and student_id = "+studentId+" ").eq("scheduling_type",1).or(j->j.eq("student_id",studentId).eq("scheduling_type",2)));
        repairCourseQueryWrapper.notInSql("id","select IFNULL(out_scheduling_detail_id,0) from repair_course where deleted=0 and course_type=4 and school_id = "+schoolId+" and year_part = 2024 and quarter= 2 and student_id = "+studentId+"");//排除已经调课出去的
        repairCourseQueryWrapper.orderByAsc("start_date_time");
        List<RepairCourse>  repairCourseList = courseSchedulingDetailMapper.getSchedulingUnionRepair(repairCourseQueryWrapper);
        return repairCourseList;
    }

    /**
     *获取排课信息
     * @return
     */
    private FaceGetScheduling getSchedulingDetail(Long schoolId,Long teacherId, Long studentId,Quarter quarter,String subjectsId,Integer lessonType){
        LocalDate nowDate = LocalDate.now();

        /*QueryWrapper<RepairCourse> repairCourseQueryWrapper = new QueryWrapper<>();
        repairCourseQueryWrapper.eq("school_date",nowDate);
        repairCourseQueryWrapper.eq("school_id",schoolId);
        repairCourseQueryWrapper.eq("deleted",0);
        repairCourseQueryWrapper.and(i->i.inSql("grade_id","select grade_id from student_grade where school_id = "+schoolId+" and status = 1 and reading_status=1 and year_part = "+quarter.getYearPart()+" and quarter_num="+quarter.getNum()+" and student_id = "+studentId+" ").eq("scheduling_type",1).or(j->j.eq("student_id",studentId).eq("scheduling_type",2)));
        repairCourseQueryWrapper.notInSql("id","select IFNULL(out_scheduling_detail_id,0) from repair_course where deleted=0 and course_type=4 and school_id = "+schoolId+" and year_part = "+quarter.getYearPart()+" and quarter="+quarter.getNum()+" and student_id = "+studentId+"");//排除已经调课出去的
        //repairCourseQueryWrapper.notInSql("id","select IFNULL(out_scheduling_detail_id,0) from course_scheduling_clash where year_part = "+quarter.getYearPart()+" and quarter_num="+quarter.getNum()+" and student_id = "+studentId+"");//精品课与智能课或者刷题班课程冲突（排除智能课）
        repairCourseQueryWrapper.orderByAsc("start_date_time");
        List<RepairCourse>  repairCourseList = courseSchedulingDetailMapper.getSchedulingUnionRepair(repairCourseQueryWrapper);*/

        String readingStatus = "1";

        Integer count = courseSchedulingDetailMapper.getSignGiveCount(quarter.getYearPart(),quarter.getNum(),nowDate.toString());
        if (count>0){
            readingStatus = "1,5";
        }
        List<RepairCourse>  repairCourseList = courseSchedulingDetailMapper.getSchedulingAndRepair(quarter.getYearPart(),quarter.getNum(),teacherId,studentId.toString(),nowDate.toString(),subjectsId,lessonType,readingStatus);

        //List<SchedulingClash> schedulingClashList = iCourseSchedulingDetailService.schedulingClash();



        FaceGetScheduling faceGetScheduling = new FaceGetScheduling();
        faceGetScheduling.setYearPart(quarter.getYearPart());
        faceGetScheduling.setQuarterNum(quarter.getNum());
        faceGetScheduling.setReachType(ReachTypeEnum.NORMAL);
        faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
        //faceGetScheduling.setLessonType(lessonType);
        //课程类别：1精品课，2智能课，3补课
        //faceGetScheduling.setCourseType(1);

        for (RepairCourse repairCourse:repairCourseList){
            if (repairCourse.getLessonType().equals(1)){
                faceGetScheduling = this.getBoutiqueFaceGetScheduling(faceGetScheduling,repairCourse,repairCourseList);
            }else {
                faceGetScheduling = this.getOtherFaceGetScheduling(faceGetScheduling,repairCourse,repairCourseList);
            }
        }



        return faceGetScheduling;
    }

    /**
     * 获取精品课排课信息
     * @param faceGetScheduling
     * @param repairCourse
     * @param repairCourseList
     * @return
     */
    public FaceGetScheduling getBoutiqueFaceGetScheduling(FaceGetScheduling faceGetScheduling,RepairCourse repairCourse,List<RepairCourse>  repairCourseList){
        LocalDate nowDate = LocalDate.now();
        LocalDateTime nowDateTime = LocalDateTime.now().withNano(0);

        RepairCourse repairCourseInfo = null;
        QueryWrapper<StudentSignInTemp> studentSignInQueryWrapper = null;
        String courseTypeName = "";

        if(faceGetScheduling.getIsToNext()){
            faceGetScheduling.setCourseType(repairCourse.getCourseType());//排课类型，1精品课（正常排课），3补课，4调课，5消课，6缺课补课,9临时排课
            if (faceGetScheduling.getCourseType().equals(3)){
                courseTypeName = "补课：";
            }else if (faceGetScheduling.getCourseType().equals(4)){
                courseTypeName = "调课：";
            }else if (faceGetScheduling.getCourseType().equals(5)){
                courseTypeName = "消课：";
            }else if (faceGetScheduling.getCourseType().equals(6)){
                courseTypeName = "缺课补课：";
            }else if (faceGetScheduling.getCourseType().equals(9)){
                courseTypeName = "临时排课：";
            }
            faceGetScheduling.setSchoolId(repairCourse.getSchoolId());
            repairCourseInfo = repairCourse;
            studentSignInQueryWrapper = new QueryWrapper<>();
            studentSignInQueryWrapper.eq("scheduling_detail_id",repairCourse.getId());
            studentSignInQueryWrapper.eq("student_id",repairCourse.getStudentId());
            studentSignInQueryWrapper.last("limit 1");

            //打卡记录
            StudentSignInTemp studentSignIn = iStudentSignInTempService.getOne(studentSignInQueryWrapper);
            //获取班级
            Grade grade = gradeMapper.selectById(repairCourse.getGradeId());
            if(grade!=null){
                faceGetScheduling.setGradeName(courseTypeName+grade.getName());
                Course course = courseMapper.selectById(grade.getCourseId());
                if(course != null){
                    faceGetScheduling.setCourseName(course.getName());
                    faceGetScheduling.setSubjectsId(course.getSubjectsId());
                }
            }
            //如果不是正常排课
            if (!faceGetScheduling.getCourseType().equals(1)){
                if(grade==null){
                    String otherName = courseTypeName;
                    Staff staff = iStaffService.getById(repairCourse.getTeacherId());
                    if (staff != null){
                        otherName += "上课老师【"+staff.getName()+"】";
                    }
                    faceGetScheduling.setGradeName(otherName);
                }
            }


            Duration duration = Duration.between(nowDateTime, repairCourseInfo.getStartDateTime());
            long minutsStart = duration.toMinutes();//上课相差分钟
            Duration durationEnd = Duration.between(nowDateTime, repairCourseInfo.getEndDateTime());
            long minutsEnd = durationEnd.toMinutes();//下课相差分钟

            faceGetScheduling.setSchedulingDetailId(repairCourse.getId());
            faceGetScheduling.setSubjectsId(repairCourse.getSubjectsId());
            faceGetScheduling.setSubjectsName(repairCourse.getSubjectsName());
            faceGetScheduling.setStatus(1);
            faceGetScheduling.setSchoolId(repairCourse.getSchoolId());

            //type类型:1签到，2签退，3已签到，4已签退，5还未到签到时间，6还未到签退时间,7超过签退时间
            if(minutsStart>60){
                faceGetScheduling.setLessonType(repairCourse.getLessonType());
                faceGetScheduling.setType(5);//还未到签到时间
                faceGetScheduling.setGradeName(faceGetScheduling.getGradeName()+"，上课时间："+repairCourseInfo.getStartDateTime()+"，请在上课前1个小时内签到");
                faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
            }else if(minutsStart<=60 && minutsEnd>10){
                if(studentSignIn == null){
                    if (minutsStart<0){
                        faceGetScheduling.setReachType(ReachTypeEnum.LATE);//迟到
                    }
                    faceGetScheduling.setLessonType(repairCourse.getLessonType());
                    faceGetScheduling.setType(1);//签到
                    if (faceGetScheduling.getLessonType().equals(1)){
                        faceGetScheduling.setIsToNext(false);//设置是否可以查找下一条
                    }else {
                        //判断是否有与精品课冲突课程
                        RepairCourse clashItem = this.getClash(repairCourse,repairCourseList);
                        if (clashItem == null){
                            faceGetScheduling.setIsToNext(false);
                        }else {
                            faceGetScheduling.setStatus(null);
                            faceGetScheduling.setIsToNext(true);
                        }
                    }
                }else {

                    faceGetScheduling.setLessonType(repairCourse.getLessonType());
                    faceGetScheduling.setType(3);//已签到
                    //faceGetScheduling.setIsToNext(false);
                    if (faceGetScheduling.getLessonType().equals(1)){
                        faceGetScheduling.setIsToNext(false);//设置是否可以查找下一条
                    }else {
                        //判断是否有与精品课冲突课程
                        RepairCourse clashItem = this.getClash(repairCourse,repairCourseList);
                        if (clashItem == null){
                            faceGetScheduling.setIsToNext(false);
                        }else {
                            faceGetScheduling.setStatus(null);
                            faceGetScheduling.setIsToNext(true);
                        }
                    }
                }
            }else if(minutsEnd >-60){
                //已签退
                if(studentSignIn != null && studentSignIn.getOutDateTime()!=null){
                    faceGetScheduling.setLessonType(repairCourse.getLessonType());
                    faceGetScheduling.setType(4);//已签退
                    faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
                }else {
                    if(studentSignIn!=null){
                        faceGetScheduling.setSignInId(studentSignIn.getId());
                    }else {
                        faceGetScheduling.setReachType(ReachTypeEnum.LATE);//迟到
                    }
                    faceGetScheduling.setLessonType(repairCourse.getLessonType());
                    faceGetScheduling.setType(2);//签退
                    //faceGetScheduling.setIsToNext(false);//设置是否可以查找下一条
                    if (faceGetScheduling.getLessonType().equals(1)){
                        faceGetScheduling.setIsToNext(false);//设置是否可以查找下一条
                    }else {
                        //判断是否有与精品课冲突课程
                        RepairCourse clashItem = this.getClash(repairCourse,repairCourseList);
                        if (clashItem == null){
                            faceGetScheduling.setIsToNext(false);
                        }else {
                            faceGetScheduling.setStatus(null);
                            faceGetScheduling.setIsToNext(true);
                        }
                    }
                }
            }else {
                //已签退
                faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
            }
        }
        return faceGetScheduling;
    }


    /**
     * 获取智能课，刷题班的排课信息
     * @param faceGetScheduling
     * @param repairCourse
     * @param repairCourseList
     * @return
     */
    public FaceGetScheduling getOtherFaceGetScheduling(FaceGetScheduling faceGetScheduling,RepairCourse repairCourse,List<RepairCourse>  repairCourseList){

        LocalDateTime nowDateTime = LocalDateTime.now().withNano(0);

        RepairCourse repairCourseInfo = null;
        QueryWrapper<StudentSignInTemp> studentSignInQueryWrapper = null;
        String courseTypeName = "";

        if(faceGetScheduling.getIsToNext()){
            faceGetScheduling.setCourseType(repairCourse.getCourseType());//排课类型，1精品课（正常排课），3补课，4调课，5消课，6缺课补课
            if (faceGetScheduling.getCourseType().equals(3)){
                courseTypeName = "补课：";
            }else if (faceGetScheduling.getCourseType().equals(4)){
                courseTypeName = "调课：";
            }else if (faceGetScheduling.getCourseType().equals(5)){
                courseTypeName = "消课：";
            }else if (faceGetScheduling.getCourseType().equals(6)){
                courseTypeName = "缺课补课：";
            }else if (faceGetScheduling.getCourseType().equals(9)){
                courseTypeName = "临时排课：";
            }
            faceGetScheduling.setSchoolId(repairCourse.getSchoolId());
            repairCourseInfo = repairCourse;
            studentSignInQueryWrapper = new QueryWrapper<>();
            studentSignInQueryWrapper.eq("scheduling_detail_id",repairCourse.getId());
            studentSignInQueryWrapper.eq("student_id",repairCourse.getStudentId());
            studentSignInQueryWrapper.last("limit 1");

            //打卡记录
            StudentSignInTemp studentSignIn = iStudentSignInTempService.getOne(studentSignInQueryWrapper);
            //获取班级
            Grade grade = gradeMapper.selectById(repairCourse.getGradeId());
            if(grade!=null){
                faceGetScheduling.setGradeName(courseTypeName+grade.getName());
                Course course = courseMapper.selectById(grade.getCourseId());
                if(course != null){
                    faceGetScheduling.setCourseName(course.getName());
                    faceGetScheduling.setSubjectsId(course.getSubjectsId());
                }
            }
            //如果不是正常排课
            if (!faceGetScheduling.getCourseType().equals(1)){
                if(grade==null){
                    String otherName = courseTypeName;
                    Staff staff = iStaffService.getById(repairCourse.getTeacherId());
                    if (staff != null){
                        otherName += "上课老师【"+staff.getName()+"】";
                    }
                    faceGetScheduling.setGradeName(otherName);
                }
            }


            Duration duration = Duration.between(nowDateTime, repairCourseInfo.getStartDateTime());
            long minutsStart = duration.toMinutes();//上课相差分钟
            Duration durationEnd = Duration.between(nowDateTime, repairCourseInfo.getEndDateTime());
            long minutsEnd = durationEnd.toMinutes();//下课相差分钟

            faceGetScheduling.setSchedulingDetailId(repairCourse.getId());
            faceGetScheduling.setSubjectsId(repairCourse.getSubjectsId());
            faceGetScheduling.setSubjectsName(repairCourse.getSubjectsName());
            faceGetScheduling.setStatus(1);

            //type类型:1签到，2签退，3已签到，4已签退，5还未到签到时间，6还未到签退时间,7超过签退时间

            //if(minutsStart>60){
            //    faceGetScheduling.setLessonType(repairCourse.getLessonType());
            //    faceGetScheduling.setType(5);//还未到签到时间
            //    faceGetScheduling.setGradeName(faceGetScheduling.getGradeName()+"，上课时间："+repairCourseInfo.getStartDateTime()+"，请在上课前1个小时内签到");
            //    faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
            //}else {
                if(studentSignIn == null){
                    if (minutsStart<0){
                        faceGetScheduling.setReachType(ReachTypeEnum.LATE);//迟到
                    }
                    faceGetScheduling.setLessonType(repairCourse.getLessonType());
                    faceGetScheduling.setType(1);//签到
                    faceGetScheduling.setIsToNext(false);//设置是否可以查找下一条
                }else {
                    faceGetScheduling.setSignInId(studentSignIn.getId());

                    Duration durationSigin = Duration.between(studentSignIn.getInDateTime(),nowDateTime);
                    long minutsSigin = durationSigin.toMinutes();//离签到相差多少分钟
                    if (repairCourse.getSubjectsId().equals(1L)){
                        //智能课暂时不签退
                        faceGetScheduling.setLessonType(repairCourse.getLessonType());
                        faceGetScheduling.setType(3);//已签到
                        faceGetScheduling.setIsToNext(false);//设置是否可以查找下一条
                    }else {
                        List<Long> signSchoolIdList = iBrushSignSchoolService.getSchoolIdList();
                        //是否需要刷题交卷功能的校区
                        if (signSchoolIdList.contains(repairCourse.getSchoolId())){
                            //刷题班
                            //离签到20分钟后才能签退
                            if (minutsSigin<20){
                                if (minutsSigin<5){
                                    faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                    faceGetScheduling.setType(3);//已签到
                                    faceGetScheduling.setIsToNext(false);//设置是否可以查找下一条
                                }else {
                                    if (!repairCourse.getSubjectsId().equals(1L)){
                                        faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                        faceGetScheduling.setType(9);//还未到交卷时间
                                        faceGetScheduling.setIsToNext(false);//
                                    }else {
                                        faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                        faceGetScheduling.setType(5);//还未签退时间
                                        faceGetScheduling.setIsToNext(false);//
                                    }
                                }
                            }else {
                                if (!repairCourse.getSubjectsId().equals(1L)){
                                    if (studentSignIn.getBrushDateTime()==null){
                                        faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                        faceGetScheduling.setType(8);//交卷
                                        faceGetScheduling.setIsToNext(false);
                                    }else {
                                        Duration durationBrush = Duration.between(studentSignIn.getBrushDateTime(),nowDateTime);
                                        long minutsBrush = durationBrush.toMinutes();//离签到相差多少分钟
                                        if (minutsBrush<5){
                                            faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                            faceGetScheduling.setType(10);//已交卷
                                            faceGetScheduling.setIsToNext(false);
                                        }else {
                                            //已签退
                                            if(studentSignIn.getOutDateTime()!=null){
                                                faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                                faceGetScheduling.setType(4);//已签退
                                                faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
                                            }else {
                                                faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                                faceGetScheduling.setType(2);//签退
                                                faceGetScheduling.setIsToNext(false);
                                            }
                                        }
                                    }
                                }else {
                                    //已签退
                                    if(studentSignIn.getOutDateTime()!=null){
                                        faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                        faceGetScheduling.setType(4);//已签退
                                        faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
                                    }else {
                                        faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                        faceGetScheduling.setType(2);//签退
                                        faceGetScheduling.setIsToNext(false);
                                    }
                                }
                            }
                        }else {
                            //离签到40分钟后才能签退
                            if (minutsSigin<40){
                                faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                faceGetScheduling.setType(3);//已签到
                                faceGetScheduling.setIsToNext(false);//设置是否可以查找下一条
                            }else {
                                //已签退
                                if(studentSignIn.getOutDateTime()!=null){
                                    faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                    faceGetScheduling.setType(4);//已签退
                                    faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
                                }else {
                                    faceGetScheduling.setLessonType(repairCourse.getLessonType());
                                    faceGetScheduling.setType(2);//签退
                                    faceGetScheduling.setIsToNext(false);
                                }
                            }
                        }

                    }

                //}
            }
        }
        return faceGetScheduling;
    }
    private RepairCourse getClash(RepairCourse repairCourseOld,List<RepairCourse>  repairCourseList){
        RepairCourse repairCourseNew = null;
        for (RepairCourse repairCourse:repairCourseList){
            if (repairCourseOld.getId().equals(repairCourse.getId())){
                continue;
            }
            if (!repairCourse.getLessonType().equals(1)){
                continue;
            }

            if ((repairCourse.getStartDateTime().isBefore(repairCourseOld.getStartDateTime()) || repairCourse.getStartDateTime().isEqual(repairCourseOld.getStartDateTime()))&&repairCourse.getEndDateTime().isAfter(repairCourseOld.getStartDateTime())){
                repairCourseNew = repairCourse;
            }else if (repairCourse.getStartDateTime().isBefore(repairCourseOld.getEndDateTime())&& (repairCourse.getEndDateTime().isAfter(repairCourseOld.getEndDateTime()) || repairCourse.getEndDateTime().isEqual(repairCourseOld.getEndDateTime()))){
                repairCourseNew = repairCourse;
            }else if (repairCourse.getStartDateTime().isBefore(repairCourseOld.getStartDateTime())&&repairCourse.getEndDateTime().isAfter(repairCourseOld.getStartDateTime())){
                repairCourseNew = repairCourse;
            }else if ((repairCourse.getStartDateTime().isAfter(repairCourseOld.getStartDateTime()) || repairCourse.getStartDateTime().isEqual(repairCourseOld.getStartDateTime()))&& (repairCourse.getEndDateTime().isBefore(repairCourseOld.getEndDateTime()) || repairCourse.getEndDateTime().isEqual(repairCourseOld.getEndDateTime()))){
                repairCourseNew = repairCourse;
            }
        }
        return repairCourseNew;
    }

    /**
     * 获取补课信息（此方法作废）
     * @return
     */
    private FaceGetScheduling getRepairCourseDetail(Long schoolId, Long studentId,Quarter quarter){
        LocalDate nowDate = LocalDate.now();
        LocalDateTime nowDateTime = LocalDateTime.now().withNano(0);

        QueryWrapper<RepairCourse> repairCourseQueryWrapper = new QueryWrapper<>();
        repairCourseQueryWrapper.eq("school_id",schoolId);
        repairCourseQueryWrapper.eq("student_id",studentId);
        //RepairCourse repairCours = repairCourseMapper.selectOne(repairCourseQueryWrapper);

        repairCourseQueryWrapper.eq("year_part",quarter.getYearPart());
        repairCourseQueryWrapper.eq("quarter",quarter.getNum());
        repairCourseQueryWrapper.eq("school_date",nowDate);
        repairCourseQueryWrapper.eq("deleted",0);
        repairCourseQueryWrapper.orderByAsc("start_date_time");
        List<RepairCourse> repairCourseList = repairCourseMapper.selectList(repairCourseQueryWrapper);

        QueryWrapper<StudentSignInTemp> studentSignInQueryWrapper = null;

        FaceGetScheduling faceGetScheduling = new FaceGetScheduling();
        faceGetScheduling.setCourseType(3);//1精品课，2智能课,3补课
        faceGetScheduling.setIsToNext(true);

        for (RepairCourse repairCourse:repairCourseList){
            if(faceGetScheduling.getIsToNext()){
                studentSignInQueryWrapper = new QueryWrapper<>();
                studentSignInQueryWrapper.eq("scheduling_detail_id",repairCourse.getId());
                studentSignInQueryWrapper.eq("student_id",studentId);
                studentSignInQueryWrapper.last("limit 1");
                //打卡记录
                StudentSignInTemp studentSignIn = iStudentSignInTempService.getOne(studentSignInQueryWrapper);


                Duration duration = Duration.between(nowDateTime, repairCourse.getStartDateTime());
                long minutsStart = duration.toMinutes();//上课相差分钟
                Duration durationEnd = Duration.between(nowDateTime, repairCourse.getEndDateTime());
                long minutsEnd = durationEnd.toMinutes();//下课相差分钟
                //type类型:1签到，2签退，3已签到，4已签退，5还未到签到时间，6还未到签退时间,7超过签退时间
                if(minutsStart>60){
                    faceGetScheduling.setType(5);//还未到签到时间
                    faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
                    break;
                }

                //获取班级
                Grade grade = gradeMapper.selectById(repairCourse.getGradeId());
                if(grade!=null){
                    faceGetScheduling.setGradeName("补课："+grade.getName());
                    Course course = courseMapper.selectById(grade.getCourseId());
                    if(course != null){
                        faceGetScheduling.setCourseName("补课："+course.getName());
                        faceGetScheduling.setSubjectsId(course.getSubjectsId());
                    }
                }

                faceGetScheduling.setSchedulingDetailId(repairCourse.getId());
                faceGetScheduling.setSubjectsId(repairCourse.getSubjectsId());
                faceGetScheduling.setSubjectsName("补课："+repairCourse.getSubjectsName());
                faceGetScheduling.setStatus(1);

                if(minutsStart<=60 && minutsEnd>10){
                    if(studentSignIn == null){
                        faceGetScheduling.setType(1);//签到
                        faceGetScheduling.setIsToNext(false);//设置是否可以查找下一条
                    }else {
                        faceGetScheduling.setType(3);//已签到
                        faceGetScheduling.setIsToNext(false);
                    }
                }else if(minutsEnd >-150){
                    //已签退
                    if(studentSignIn != null && studentSignIn.getOutDateTime()!=null){
                        faceGetScheduling.setType(4);//已签退
                        faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
                    }else {
                        faceGetScheduling.setType(2);//签退
                        faceGetScheduling.setIsToNext(false);//设置是否可以查找下一条
                    }
                }else {
                    //已签退
                    if(studentSignIn != null && studentSignIn.getOutDateTime()!=null){
                        faceGetScheduling.setType(4);//已签退
                        faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
                    }else {
                        faceGetScheduling.setType(2);//签退
                        faceGetScheduling.setIsToNext(false);//设置是否可以查找下一条
                    }
                    //faceGetScheduling.setIsToNext(true);//设置是否可以查找下一条
                }
            }
        }
        return faceGetScheduling;
    }
}
